package com.kyzh.core.activities

import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Gift
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemMygiftBinding
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.copy
import com.kyzh.core.utils.startActivity
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.activity_mygift.*

class MyGiftActivity : BaseActivity() {

    private val beans = arrayListOf<Gift>()
    private val adapter = Adapter(R.layout.item_mygift,beans)
    private var p =1
    private var max =2
    private var type = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mygift)
        type = intent.getIntExtra(GlobalKeys.TYPE,0)
        initView()
    }

    private fun initView() {
        if (type==0){
            titleView.setText(getString(R.string.myGift))
            tvHistory.visibility = View.VISIBLE
        }else{
            titleView.setText(getString(R.string.expiredGift))
            tvHistory.visibility = View.GONE
        }
        tvHistory.setOnClickListener {
            startActivity<MyGiftActivity>(GlobalKeys.TYPE to 1)
        }
        rvList.adapter = adapter
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        root.setOnRefreshListener {
            refresh()
            root.isRefreshing = false
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<Gift>):BaseQuickAdapter<Gift,BaseDataBindingHolder<ItemMygiftBinding>>(layout,beans), LoadMoreModule {
        override fun convert(holder: BaseDataBindingHolder<ItemMygiftBinding>, item: Gift) {
            holder.dataBinding?.let {
                it.data = item
                if (item.status=="0"){
                    it.tvCopy.visibility = View.VISIBLE
                    it.ivDelete.visibility = View.GONE
                }else{
                    it.ivDelete.visibility = View.VISIBLE
                    it.tvCopy.visibility = View.GONE
                }
                it.tvCopy.setOnClickListener {
                    item.card_no.copy()
                    getString(R.string.copyGiftCode).toast()
                }
                it.ivDelete.setOnClickListener {
                    alert(getString(R.string.deleteGift),getString(R.string.sureToDelete),getString(R.string.sure),getString(R.string.cancel),{
                        UserRequest.myGiftDelete(item.id){
                            beans.remove(item)
                            notifyDataSetChanged()
                        }
                    },{

                    })
                }
            }

        }

    }



    fun getData(){
        UserRequest.myGift(type,p){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        UserRequest.myGift(type,1){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
}